package cn.marknum.gateway.filter;//package springcloud;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.util.Arrays;
//import java.util.List;
//
///**
// * @Description:
// * @Author: wangxiaohong
// * @Date: 14:11 2019-08-22
// */
//public class RequestTimeGatewayFilterFactory extends AbstractGatewayFilterFactory<RequestTimeGatewayFilterFactory.Config> {
//
//    private static final Logger log = LoggerFactory.getLogger(RequestTimeGatewayFilterFactory.class);
//    private static final String REQUEST_TIME_BEGIN = "requestTimeBegin";
//    private static final String KEY = "withParams";
//
//
//    @Override
//    public List<String> shortcutFieldOrder() {
//        return Arrays.asList(KEY);
//    }
//
//    public RequestTimeGatewayFilterFactory() {
//        super(Config.class);
//    }
//
//    @Override
//    public GatewayFilter apply(Config config) {
//        return (exchange, chain) -> {
//            exchange.getAttributes().put(REQUEST_TIME_BEGIN, System.currentTimeMillis());
//            log.info("=====请求开始=====");
//            return chain.filter(exchange).then(
//                    Mono.fromRunnable(() -> {
//                        Long startTime = exchange.getAttribute(REQUEST_TIME_BEGIN);
//                        if (startTime != null) {
//                            StringBuilder sb = new StringBuilder(exchange.getRequest().getURI().getRawPath())
//                                    .append(": ")
//                                    .append(System.currentTimeMillis() - startTime)
//                                    .append("ms");
//                            if (config.isWithParams()) {
//                                sb.append(" params:").append(exchange.getRequest().getQueryParams());
//                            }
//                            log.info(sb.toString());
//                        }
//                    })
//            );
//        };
//    }
//
//    public static class Config {
//
//        private boolean withParams;
//
//        public boolean isWithParams() {
//            return withParams;
//        }
//
//        public void setWithParams(boolean withParams) {
//            this.withParams = withParams;
//        }
//
//    }
//}
